<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;

class TaskLoadExport implements FromCollection, WithHeadings, WithColumnWidths, WithEvents
{
    protected $data;

    public function __construct(Collection $data)
    {
        $this->data = $data;
    }

    public function collection()
    {
        return $this->data;
    }

    public function headings(): array
    {
        return [
            '客户',
            '入仓单号',
            '目的地',
            '货物属性',
            '大包件数',
            '重量',
            '配载进度',
            '包装',
        ];

    }

    public function columnWidths(): array
    {
        // 设置每列的宽度
        return [
            'A' => 30,
            'B' => 50,
            'C' => 50,
            'D' => 50,
            'E' => 20,
            'F' => 30,
            'G' => 30,
            'H' => 30,
            'I' => 30,
            'J' => 20,
            // 根据你的需求设置其他列的宽度
        ];
    }

    public function registerEvents(): array
    {
        return [
            BeforeSheet::class => function(BeforeSheet $event) {

                // 安全获取第一行数据
                $data = $this->data->toArray();
                $firstRowData = isset($data[0]) && is_array($data[0]) ? $data[0] : [];

                $event->sheet->getDelegate()->setCellValue('A1', '车牌号');
                $event->sheet->getDelegate()->getStyle('A1')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B1', $firstRowData['car_number'] ?? '');

                $event->sheet->getDelegate()->setCellValue('A2', '车型');
                $event->sheet->getDelegate()->getStyle('A2')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B2', $firstRowData['car_type'] ?? '');

                $event->sheet->getDelegate()->setCellValue('A3', '内的司机');
                $event->sheet->getDelegate()->getStyle('A3')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B3', $firstRowData['driver'] ?? '');

                $event->sheet->getDelegate()->setCellValue('A4', '香港司机');
                $event->sheet->getDelegate()->getStyle('A4')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B4', $firstRowData['hk_driver'] ?? '');

                $event->sheet->getDelegate()->setCellValue('A5', '封关场');
                $event->sheet->getDelegate()->getStyle('A5')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B5', $firstRowData['feng'] ?? '');

                $event->sheet->getDelegate()->setCellValue('A6', '大包件数');
                $event->sheet->getDelegate()->getStyle('A6')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B6', $firstRowData['big_pkgs'] ?? '');
                $event->sheet->getDelegate()->getStyle('B6')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);

                $event->sheet->getDelegate()->setCellValue('A7', '装车点');
                $event->sheet->getDelegate()->getStyle('A7')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B7', $firstRowData['load_warehouse'] ?? '');

                $event->sheet->getDelegate()->setCellValue('A8', '卸货点');
                $event->sheet->getDelegate()->getStyle('A8')->getFont()->setBold(true);
                $event->sheet->getDelegate()->setCellValue('B8', $firstRowData['unload_warehouse'] ?? '');

                $event->sheet->getDelegate()->setCellValue('A9', '');
                $event->sheet->getDelegate()->setCellValue('A10', '');



                // 从数据中移除第一行（航班信息），只保留提单信息
                if (count($data) > 0) {
                    unset($data[0]);
                    $this->data = collect(array_values($data));
                }

//                $sheet = $event->sheet->getDelegate();
//                $sheet->getRowDimension(1)->setRowHeight(25);
//                $sheet->getRowDimension(2)->setRowHeight(25);
//                $sheet->getRowDimension(3)->setRowHeight(25);
            },

            AfterSheet::class => function(AfterSheet $event) {
                // 设置表头（第11行）字体加粗
                $event->sheet->getDelegate()->getStyle('A11:H11')
                    ->getFont()->setBold(true);
            },
        ];
    }

}